package com.project.blog.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.blog.common.Result;
import com.project.blog.dto.ArticleDTO;
import com.project.blog.dto.PageInfo;
import com.project.blog.entity.Article;
import com.project.blog.entity.Category;
import com.project.blog.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author 代码奔腾
 * @since 2022-09-02
 */
@Api(tags="博客文章模块")
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {
    @ApiOperation(value = "查询所有文章", notes="查询所有文章")
    @GetMapping("/")
    public List<Article> list() {
        return articleService.list();
    }

    @ApiOperation(value = "查询文章（带分页）", notes="查询文章（带分页）")
    @PostMapping("/page")
    public Result<?> findPage(@RequestBody PageInfo pageInfo) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Article::getId);
        Page<Article> page = articleService.page(new Page<>(
                        pageInfo.getPageNum(),
                        pageInfo.getPageSize()),
                wrapper
        );
        return Result.success(page);
    }
    @ApiOperation(value = "添加文章", notes="添加文章")
    @PostMapping("/save")
    public Object save(@Validated @RequestBody Article article) {
        articleService.saveOrUpdate(article);
        return Result.success();
    }
    @ApiOperation(value = "删除文章", notes="删除文章")
    @PostMapping("/delBatch")
    public Object deleteBatch(@RequestBody List<Integer> ids) {
        articleService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation(value = "文章详情", notes="文章详情")
    @GetMapping("/detail")
    public Result<?> detail(@RequestParam("id") Integer id) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getId, id)
                .last("limit 1");
        Article article = articleService.getOne(wrapper);
        if (BeanUtil.isEmpty(article)) {
            return Result.error("没有找到这条数据");
        } else {
            //浏览量+1
            if (article.getViews() == null) {
                article.setViews(0);
            } else {
                article.setViews(article.getViews() + 1);
            }
            ArticleDTO articleDTO = new ArticleDTO();
            BeanUtil.copyProperties(article, articleDTO);
            //查询出发布人信息
            if (article.getUserId() != null) {
                User userInfo = userService.getById(article.getUserId());
                if (userInfo != null) {
                    articleDTO.setUsername(userInfo.getUsername());
                }
            }
            //查询出文章栏目
            if (article.getCategoryId() != null) {
                Category categoryInfo = categoryService.getById(article.getCategoryId());
                if (categoryInfo != null) {
                    articleDTO.setCategoryTitle(categoryInfo.getTitle());
                }
            }

            //浏览器+1
            articleService.updateById(article);
            return Result.success(articleDTO);
        }
    }
}
